 
#include <stdio.h>  
#include <sys/types.h>  
#include <sys/socket.h>  
#include <sys/un.h>   
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>

#include<unistd.h>

#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<errno.h>
#include<sys/un.h>
#include<stdio.h>


#include <pthread.h>
#include <sys/time.h>
   
#define CAN_SERVICE "CAN_SERVICE" 

unsigned char data_ptr[1920*1080*3] = { 0 };
unsigned char data_t[1024*1024*10] = { 0 };

typedef struct head_s
{
    /* data */
    int index;
    int size;
    int checksum;
    unsigned char ptr[1920*1080*3];
};


typedef struct msg_head
{
    /* data */
    int head;
    int sum;
    int index;
    int length;
    int checksum;
};

int publish(int fd, int size, unsigned char* data)
{
    int rest = size;
    msg_head head = { 0 };
    head.head = 0x80;
    head.length = size;

    int pack_len = 10000;

    int temp = size % pack_len;
    if (temp == 0)
    {
        /* code */
        head.sum = temp;
    }
    else
    {
        head.sum = size / pack_len + 1;
    }
    int sum_all = 0;
    for(int i = 0; i < head.sum; i++)
    {
        /* code */
        // printf("size:%d\n", size);
        unsigned char data_temp[11000] = { 0 };

        head.index = i;
        int head_len = sizeof(head);
         
        memcpy(data_temp, &head, head_len);
        if(i == (head.sum - 1))
        {
            memcpy(data_temp + head_len, data + i * pack_len, temp);
            int len = write(fd, data_temp, head_len + temp); 
            sum_all += len;
        }
        else
        {
            memcpy(data_temp + head_len, data + i * pack_len, pack_len);
            int len = write(fd, data_temp, head_len + pack_len);
            sum_all += len; 
        }
        
        // usleep(10);
    }
    printf("head.sum:%d send len:%d\n", head.sum, sum_all);
    return rest;
}


void *fun(void *arg)
{
	printf("I'm thread, Thread ID = %lu\n", pthread_self());
    int socket_fd = *(int*)arg;
    int count = 0;
    while (1)
    {
        /* code */
                // 读取和写入  
        // char recv_buf[1024];
        // memset(recv_buf,0,1024);  
        // int num=read(socket_fd,recv_buf,sizeof(recv_buf));  
        // if(num <= 0)
        // {
        //     printf("client close\n");
        //     return NULL;
        // }
        // printf("[%lu]Message from client (%d)) :%s\n", pthread_self(), num,recv_buf);   
        head_s data = { 0 };
        data.index = count++;
        data.size = 1920*1080*3;
        data.checksum = 1920*1080*3;
        // unsigned char data_ptr[1920];
        memcpy(data.ptr, data_ptr, 1920*1080*3);
        // unsigned char temp_data[1024*1024*1024] = { 0 };
        // memcpy(temp_data, &data, sizeof(data));
        // printf("sizeof(temp_data):%d\n", sizeof(temp_data));
        // memcpy(data_t, &data, sizeof(data));
        struct timeval begin;
        gettimeofday(&begin,NULL);
        long long beginTime = (long long)begin.tv_sec * 1000 + (long long)begin.tv_usec / 1000;

        int send_len = publish(socket_fd, sizeof(data), (unsigned char*)&data);

        struct timeval end;
        gettimeofday(&end,NULL);
        long long endTime = (long long)end.tv_sec * 1000 + (long long)end.tv_usec / 1000;
        printf("send len:%d   sizeof(data):%d   use time:%ld ms\n", send_len, sizeof(data), endTime - beginTime);
        usleep(30000);
    }


	return NULL;
}


int main(void)  
{      
    int ret;     
    int len;  
    int accept_fd;
    int socket_fd; 
    static char recv_buf[1024];
    struct sockaddr_un clt_addr;  
    struct sockaddr_un srv_addr;  
    socklen_t clt_addr_len = sizeof(clt_addr);
 
    socket_fd=socket(PF_UNIX,SOCK_STREAM,0);  
    if(socket_fd<0)  
    {  
        perror("cannot create communication socket");  
        return 1;  
    }    
          
    // 设置服务器参数  
    srv_addr.sun_family=AF_UNIX;  
    strncpy(srv_addr.sun_path,CAN_SERVICE,sizeof(srv_addr.sun_path)-1);  
    unlink(CAN_SERVICE);  
 
    // 绑定socket地址 
    ret=bind(socket_fd,(struct sockaddr*)&srv_addr,sizeof(srv_addr));  
    if(ret==-1)  
    {  
        perror("cannot bind server socket");  
        close(socket_fd);  
        unlink(CAN_SERVICE);  
        return 1;  
    }  
 
    // 监听   
    ret=listen(socket_fd,1);  
    if(ret==-1)  
    {  
        perror("cannot listen the client connect request");  
        close(socket_fd);  
        unlink(CAN_SERVICE);  
        return 1;  
    }  

    while (1)
    {

    
        // 接受connect请求 
        len=sizeof(clt_addr);  
        accept_fd=accept(socket_fd,(struct sockaddr*)&clt_addr,&clt_addr_len);  
        if(accept_fd<0)  
        {  
            perror("cannot accept client connect request");  
            // close(socket_fd);  
            // unlink(CAN_SERVICE);  
            // return 1;  
        }  

        pthread_t tid;
 
	    pthread_create(&tid, NULL, fun, &accept_fd);
    
        // 读取和写入  
        // memset(recv_buf,0,1024);  
        // int num=read(accept_fd,recv_buf,sizeof(recv_buf));  
        // printf("Message from client (%d)) :%s\n",num,recv_buf);    
                /* code */
    } 
    // 关闭socket
    close(accept_fd);  
    close(socket_fd);  
    unlink(CAN_SERVICE);  
    return 0;  
}  